// function add(x: number, y: number) : number{
function add(x: number, y: number) {
  return x + y;
}

interface IAdd {
  (x: number, y: number): number;
}
// const add1: IAdd = (
const add1: (x: number, y: number) => number = (
  x: number,
  y: number
): number => {
  return x + y;
};

const add2: IAdd = (x: number, y: number, ...args: number[]): number => {
  return x + y + args.reduce((a, b) => a + b, 0);
};

const add3: IAdd = function (x: number, y: number): number {
  return x + y;
};

let num1 = 12;
// num1 = "12"; // 报错，类型不匹配

// 函数重载
function join(str: string): string;
function join(strs: string[]): string;
function join(str: string | string[]): string {
  if (Array.isArray(str)) {
    return str.join(",");
  }
  if (typeof str === "string") {
    return str;
  }
  return "";
}

// 类型守卫 + 继承
interface IAnimal {
  name: string;
  move(): void;
}

class Animal implements IAnimal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  move(): void {
    console.log(this.name + " is moving");
  }
}

class Bird extends Animal {
  constructor(name: string) {
    super(name);
  }
  fly(): void {
    console.log(this.name + " is flying");
  }
}

class Fish extends Animal {
  constructor(name: string) {
    super(name);
  }
  swim(): void {
    console.log(this.name + " is swimming");
  }
}

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }
  bark(): void {
    console.log(this.name + " is barking");
  }
}

let animal: IAnimal = new Bird("bird");

let animal1: IAnimal = new Fish("fish");

// const isBird = (animal: IAnimal): animal is Bird => {
const isBird = (animal: IAnimal): string => {
  if (animal instanceof Bird) {
    return "is Bird";
  } else if (animal instanceof Fish) {
    return "is Fish";
  } else if (animal instanceof Dog) {
    return "is Dog";
  }
  else {
    return "is not Bird or Fish";
  }
  // return (animal as Bird).fly !== undefined;
  // return animal instanceof Bird;
};
console.log(isBird(animal1));
